package cn.acewill.yxd.yunxundian.ui.user.Presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iflytek.cloud.thirdparty.T;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.user.Contract.IpconfigListener;
import cn.acewill.yxd.yunxundian.ui.user.Contract.LoginLModel;
import cn.acewill.yxd.yunxundian.ui.user.Contract.LoginListener;
import cn.acewill.yxd.yunxundian.ui.user.Contract.LoginPresenter;
import cn.acewill.yxd.yunxundian.ui.user.Contract.LoginView;

/**
 * Created by xxx on 2018/1/9.
 * model 与 view 的交互
 */

public class LoginPresenterIml implements LoginPresenter,LoginListener,IpconfigListener{
    private LoginView loginView;
    private LoginLModel loginModel;
    LoginPresenterIml(LoginView loginView) {
        this.loginView = loginView;
        this.loginModel = new LoginModelIml();
    }

    @Override
    public void vaildValue(String username, String password,String ipName) {
        if (loginView!=null){
//            if (TextUtils.isEmpty(ipName)){
//                loginView.shakeIPCheck();
//                return;
//            }
//            if (TextUtils.isEmpty(username)){
//                loginView.shakeUserName();
//                return;
//            }
//            if (TextUtils.isEmpty(password)) {
//                loginView.shakeUserPass();
//                return;
//            }
            loginView.showProgress();
            loginModel.onUserLogin(username,password,ipName,this);
        }
    }

    @Override
    public void ipCheck(String code, String pwd) {
        if (loginView!=null){
            if (TextUtils.isEmpty(code)||TextUtils.isEmpty(pwd)){
                loginView.inputAllInfo();
                return;
            }
            loginView.showProgress();
            loginModel.checkIpAvild(code,pwd,this);
        }
    }


    @Override
    public void onDestory() {
        loginView = null;
    }

    @Override
    public void onLoginSuccess(String result) {
        loginView.hideProgress();
        String[] strings=result.split("\\|");
        if (strings[0].equals("200")){
            Gson gson = new Gson();
            Type typeList = new TypeToken<ArrayList<Employee>>() {
            }.getType();
            ArrayList<Employee> emps = gson.fromJson(pubClass.convertToArrayJson(result), typeList);
            loginView.clearShopAndScheduleData();
            loginView.saveEmp();
            loginView.returnEmpList(emps);
            loginView.toMainActivity();
        }else if (strings[0].equals("201")){
            if(strings[1].equals("用户名不存在")){
                loginView.shakeUserName();
            }else{
                loginView.shakeUserPass();
            }
        }
    }

    @Override
    public void onLoginFailed() {
        Log.e("LoginPreIml","----erro");
        loginView.hideProgress();
    }

    @Override
    public void onRequestSuccess(String reslut) {
        loginView.hideProgress();
        String[] array = reslut.split("\\|");
        if (array[0].equals("200")){
            loginView.ipConfigOk();
            loginView.returnIpConfig(array[3],array[4]);
        }else{
            loginView.ipCnonfigFailed();
            loginView.hideProgress();
        }
    }

    @Override
    public void onRequestErro() {
        loginView.hideProgress();
    }



}
